using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.Loon.Framework.Xna.Java.Generics;
using Org.Loon.Framework.Xna.Game.Core.Graphics.OpenGL;

namespace Org.Loon.Framework.Xna.Game.Action.Scripting.Pack
{
    public class PackSprites
    {

        public interface PackSpriteListener
        {

            void Update(PackSprite spr);

        }

        private PackSpriteListener listener;

        private List<PackSprite> sprites;

        private PackView view;

        public PackSprites(PackView view)
        {
            SetView(view);
        }

        public PackSprites()
        {
            SetView(null);
        }

        public void SetView(PackView v)
        {
            if (sprites == null)
            {
                sprites = new List<PackSprite>(10);
            }
            this.view = v;
            if (view == null)
            {
                view = PackView.EmptyView.GetInstance();
            }
        }

        public PackView GetView()
        {
            return view;
        }

        public int Size()
        {
            return sprites.Count;
        }

        public JavaListInterface.IIterator<PackSprite> elements()
        {
            return new IteratorAdapter<PackSprite>(sprites.GetEnumerator());
        }

        public void Add(PackSprite sprite, float x, float y)
        {
            sprite.SetX(x);
            sprite.SetY(y);
            Add(sprite);
        }

        public void Add(PackSprite sprite)
        {
            sprites.Add(sprite);
        }

        public void Remove(PackSprite sprite)
        {
            sprites.Remove(sprite);
        }

        public void Clear()
        {
            sprites.Clear();
        }

        public PackSprite Find(String name)
        {
            for (int i = 0; i < sprites.Count; i++)
            {
                PackSprite s = sprites[i];
                if (s == null)
                {
                    return null;
                }
                if (name.Equals(s.GetName(), StringComparison.InvariantCultureIgnoreCase))
                {
                    return s;
                }
            }
            return null;
        }

        public void Draw(LTexturePack pack, long elapsedTime)
        {
            for (int i = 0; i < sprites.Count; i++)
            {
                PackSprite s = sprites[i];
                if (s == null || !s.visible)
                {
                    return;
                }
                if (listener != null)
                {
                    listener.Update(s);
                }
                s.Update(elapsedTime);
                s.Draw(pack, view);
            }
        }

        public PackSpriteListener GetListener()
        {
            return listener;
        }

        public void SetListener(PackSpriteListener listener)
        {
            this.listener = listener;
        }

    }

}
